package com.unicom.projt.account;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.AlertDialogUtils;
import com.unicom.projt.util.PageJumpUtils;
import com.unicom.projt.util.SystemUtils;
import com.unicom.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 下午2:35
 */
public class ChangePasswordPage extends Activity implements View.OnClickListener {

    private EditText mOldPwd;
    private EditText mNewPwd;
    private EditText mRepeatPwd;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 2;
    private ChangePasswordController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password);

        mOldPwd = (EditText) this.findViewById(R.id.change_password_current_pwd);
        mNewPwd = (EditText) this.findViewById(R.id.change_password_new_pwd);
        mRepeatPwd = (EditText) this.findViewById(R.id.change_password_rp_pwd);



        this.findViewById(R.id.change_password_submit_btn).setOnClickListener(this);

        this.findViewById(R.id.change_password_back_btn).setOnClickListener(this);
        this.findViewById(R.id.change_password_logout_btn).setOnClickListener(this);
        mController=new ChangePasswordController(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.change_password_submit_btn:
                submitNewPassword();
                break;
            case R.id.change_password_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.change_password_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;

        }
    }

    public void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void submitNewPassword() {

        String oldPwd=mOldPwd.getText().toString().trim();
        String newPwd=mNewPwd.getText().toString().trim();
        String rtPwd=mRepeatPwd.getText().toString().trim();
        if("".equals(oldPwd) || "".equals(newPwd) || "".equals(rtPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"请填写完整");
            return;
        }
        if(newPwd.length()<6){
            AlertDialogUtils.showConfirmDiaLog(this,"密码最少要6位");
            return;
        }
        if(!newPwd.equals(rtPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"两次密码输入不一致");
            return;
        }
        showDialog(DIALOG_PROGRESS);
        mController.submitNewPassword(UserHolder.phoneNumber,oldPwd,newPwd);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
